<?php
/*code here*/
$db = new Database();
$fs = new functions();
    /* Redirect user if they already logged */
    if(Auth::hasAuth('current_user'))
    {
        //$this->view->current_user = Auth::getAuth('current_user');
        if($_GET['type']=="go"){
            $location = $_GET['redirect'];
            $redirect = str_replace("|=%=|", "&", $location);
            header('Location: ' . $redirect);
        }else{
            header('location: /home');
        }
        
    }

    /* For user login if correct redirect to home */
    if($_SERVER['REQUEST_METHOD'] == "POST")
    {
        $username = $_POST['username'];
        $password = $_POST['password'];
        $session = new Auth();
        $login = $session->login($username,$password,'email','password','tbuser');
        if($login){
            $date = $fs->currentDateTime();
            $auth = Auth::getAuth('current_user');
            // Audit Logs
            $insert_audit_rec = array("user_id"=>$auth['id'],
                                      "audit_action"=>"2",
                                      "table_name"=>"tbuser",
                                      "record_id"=>$auth['id'],
                                      "date"=>$date,
                                      "ip"=>$_SERVER["REMOTE_ADDR"],
                                      "is_active"=>1);
            $audit_log = $db->insert("tbaudit_logs",$insert_audit_rec);
            //header('Location: /home');
            $personDoc = new Person($db,$login['id']);
            $personDoc->is_available = '1';
            $personDoc->update();
            
            // Reset Timeout idle
                $personDoc = new Person($db,$auth['id']);
                $personDoc->timeout = '0';
                $personDoc->update();
                
            if($_GET['type']=="go"){
                $location = $_GET['redirect'];
                $redirect = str_replace("|=%=|", "&", $location);
                header('Location: ' . $redirect);
            }else{
                $current_page = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
                
                $getPage = $db->query("SELECT * FROM page_settings WHERE userID={$db->escape($login['id'])} AND is_active={$db->escape(1)}","row");
                $getPage_count = $db->query("SELECT * FROM page_settings WHERE userID={$db->escape($login['id'])} AND is_active={$db->escape(1)}","numrows");
                if($getPage_count==0){
                    header('location: /');
                }else{
                    header('location: ' . $getPage['page']);
                }
            }
        }else{
            if($_GET['type']=="go"){
                $path = functions::curPageURL("");
                $get_module_param = explode("?",$path);
                header('Location: /login?' . $get_module_param[1] . '?' . $get_module_param[2]);
            }else{
                header('Location: /login?login_attemp=1'); 
            }
        }
    }  
/*code here(end)*/
$this->setLayout();